﻿using System;
using System.Collections.Generic;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using Dal;
using NLog;

namespace ADONetDal
{

    public class ADONetRepositoryVaccina : ADONetRepositoryBase<Vaccina> 
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Vaccina entity)
        {
            var cmd = new SqlCommand("VaccinaInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("VacDate", entity.VacDate));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("PatientId", entity.Patient.Id));
            logger.Trace("SqlCommand для записи данных Вакцины в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("VaccinaDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Вакцины id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Vaccina entity)
        {
            var cmd = new SqlCommand("VaccinaUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("VacDate", entity.VacDate));
            cmd.Parameters.Add(new SqlParameter("Name", entity.Name));
            cmd.Parameters.Add(new SqlParameter("PatientId", entity.Patient.Id));
            logger.Trace("SqlCommand для обновления данных Вакцины id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetVaccina", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Вакцины id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            var cmd = new SqlCommand("GetVaccinaList", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            logger.Trace("SqlCommand для выборки всех существующих Вакцин сформирован");
            return cmd;
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetVaccinaByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Вакцины {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Vaccina GetEntity(SqlDataReader sqlDataReader)
        {
            Vaccina vaccina = new Vaccina();
            IRepository<Patient> adoNetRepositoryPatient = new ADONetRepositoryPatient();
            vaccina.Id = (int)sqlDataReader["Id"];
            vaccina.VacDate = (DateTime)sqlDataReader["VacDate"];
            vaccina.Name = sqlDataReader["Name"].ToString();
            vaccina.Patient = adoNetRepositoryPatient.GetById((int)sqlDataReader["PatientId"]);
            return vaccina;
        }
        
    }
}
